/**
 * ajax页面校验用（全局）
 * created by amon.lei
 */

 // 校验结果
 var validated = true;

 var firstErrorObj = null;

 function initValidate()
 {
     //firstErrorObj = null;
     validated = true;
 }

 // 是否通过
 function isAllValidated()
 {
     return validated;
 }
 
 // 校验通过
 function validated()
 {
     
 }

 // 校验通不过
 function inValidated()
 {
     validated = validated & false;
 }

 // 错误信息显示
 function showErrMessage(objId,spanId,errorMessage)
 {
     inValidated();
    if(firstErrorObj == null)
    {
        firstErrorObj = $(objId);        
    }
    $(spanId).innerHTML = "<font color='red'>" + errorMessage + "</font>";
    if(firstErrorObj == $(objId))
    {
        firstErrorObj.focus();
        firstErrorObj.select();
    }
 }

 function showErrMessageByDefaultSpan(objId,errorMessage)
 {
     showErrMessage(objId,objId + "Span",errorMessage);
 }

 // 清楚错误信息
 function cleanSpan(spanIds)
 {
     for(var i = 0; i < spanIds.length; i ++)
     {
         $(spanIds[i]).innerHTML = "";
     }
 }

// 键盘输入的时候停止切换到其他控件
function stopUnblurWhenKeyUp()
{
    Form.getElements("detailForm").each
        (
            function(element)
            {
                Event.observe(element,"keyup",handlerOnKeyUpEvent,false);
               
            }
        );
}

function handlerOnKeyUpEvent(event)
{
    var element = Event.element(event);
    firstErrorObj = element;
    if(event.keyCode == Event.KEY_RETURN)
    {
        firstErrorObj = null;
        try
        {
            validate();
        }
        catch (e)
        {
            alert("调用validate()方法失败");
        }
    }
}